package cn.gx.kevin.service.impl;
import cn.gx.kevin.dao.IUserDao;
import cn.gx.kevin.domain.Menu;
import cn.gx.kevin.domain.User;
import cn.gx.kevin.domain.UserRole;
import cn.gx.kevin.service.IUserService;
import cn.gx.kevin.dao.IBaseDao;
import cn.gx.kevin.service.impl.BaseService;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
*Generate by Kevin's CodeBuilder
**/
@Service
public class UserService extends BaseService<User, String> implements IUserService {

		private static final long serialVersionUID = -1L;

		private IUserDao userDao;

		@Resource(name="userDao")
		@Override
		public void setDao(IBaseDao<User, String> dao) {
				this.dao = dao;
				userDao = (IUserDao)dao;
		}




	@Override
		public void insertSuperAdmin(String name, String zhName, String pwd) throws Exception {
			userDao.insertSuperAdmin(name, zhName, pwd);
		}

		@Override
		public User loadUserData(String userId) throws Exception {
			Map<String,Object> params = new HashMap<String,Object>();
			params.put("id", userId);
			return userDao.loadUserData(params);
		}

		@Override
		public void updatePwd(String[] splitIds, String newpwd) throws Exception {
			Map<String,Object> prs = new HashMap<String,Object>();
			prs.put("userPwd", newpwd);
			prs.put("idList", splitIds);
			this.userDao.updateParam(prs);
		}

		@Override
		public int insert(User entity) throws Exception {
			String pwd = entity.getUserPwd();
			if(StringUtils.isNotEmpty(pwd) && pwd.length() < 32){
				entity.setUserPwd(DigestUtils.md5Hex(pwd));
			}
			return super.insert(entity);
		}

	@Override
	public int insertNotNull(User entity) throws Exception {
		String pwd = entity.getUserPwd();
		if(StringUtils.isNotEmpty(pwd) && pwd.length() < 32){
			entity.setUserPwd(DigestUtils.md5Hex(pwd));
		}
		return super.insertNotNull(entity);
	}


	@Override
	public Map<String, List<UserRole>> selectUserRole(List<String> userIds) throws Exception {
		return this.userDao.selectUserRole(userIds);
	}
}
